import bootbox from 'bootbox';

angular.module('portainer.app').factory('ModalService', [
  '$sanitize',
  function ModalServiceFactory($sanitize) {
    'use strict';
    var service = {};

    var applyBoxCSS = function (box) {
      box.css({
        top: '50%',
        'margin-top': function () {
          return -(box.height() / 2);
        },
      });
    };

    var confirmButtons = function (options) {
      var buttons = {
        confirm: {
          label: $sanitize(options.buttons.confirm.label),
          className: $sanitize(options.buttons.confirm.className),
        },
        cancel: {
          label: options.buttons.cancel && options.buttons.cancel.label ? $sanitize(options.buttons.cancel.label) : '取消',
        },
      };
      return buttons;
    };

    service.enlargeImage = function (image) {
      image = $sanitize(image);
      bootbox.dialog({
        message: '<img src="' + image + '" style="width:100%" />',
        className: 'image-zoom-modal',
        onEscape: true,
      });
    };

    service.confirmWebEditorDiscard = confirmWebEditorDiscard;
    function confirmWebEditorDiscard() {
      const options = {
        title: 'Are you sure ?',
        message: 'You currently have unsaved changes in the editor. Are you sure you want to leave?',
        buttons: {
          confirm: {
            label: 'Yes',
            className: 'btn-danger',
          },
        },
      };
      return new Promise((resolve) => {
        service.confirm({ ...options, callback: (confirmed) => resolve(confirmed) });
      });
    }

    service.confirmAsync = confirmAsync;
    function confirmAsync(options) {
      return new Promise((resolve) => {
        service.confirm({ ...options, callback: (confirmed) => resolve(confirmed) });
      });
    }

    service.confirm = function (options) {
      var box = bootbox.confirm({
        title: options.title,
        message: options.message,
        buttons: confirmButtons(options),
        callback: options.callback,
      });
      applyBoxCSS(box);
    };

    function prompt(options) {
      var box = bootbox.prompt({
        title: options.title,
        inputType: options.inputType,
        inputOptions: options.inputOptions,
        buttons: confirmButtons(options),
        callback: options.callback,
      });
      applyBoxCSS(box);
    }

    function customPrompt(options, optionToggled) {
      var box = bootbox.prompt({
        title: options.title,
        inputType: options.inputType,
        inputOptions: options.inputOptions,
        buttons: confirmButtons(options),
        callback: options.callback,
      });
      applyBoxCSS(box);
      box.find('.bootbox-body').prepend('<p>' + options.message + '</p>');
      box.find('.bootbox-input-checkbox').prop('checked', optionToggled);
    }

    service.confirmAccessControlUpdate = function (callback) {
      service.confirm({
        title: '你确定吗？',
        message: '改变该资源的所有权将有可能限制某些用户对该资源的管理。',
        buttons: {
          confirm: {
            label: '更改所有权',
            className: 'btn-primary',
          },
        },
        callback: callback,
      });
    };

    service.confirmImageForceRemoval = function (callback) {
      service.confirm({
        title: '你确定吗？',
        message: '强制删除镜像，即使它有多个标签，或者它被停止的容器使用，该镜像都将被删除',
        buttons: {
          confirm: {
            label: '移除镜像',
            className: 'btn-danger',
          },
        },
        callback: callback,
      });
    };

    service.cancelRegistryRepositoryAction = function (callback) {
      service.confirm({
        title: '你确定吗？',
        message: '警告：在此操作完成之前中断操作，将导致所有标签丢失。你确定你要这样做吗？',
        buttons: {
          confirm: {
            label: '停止',
            className: 'btn-danger',
          },
        },
        callback: callback,
      });
    };

    service.confirmDeletion = function (message, callback) {
      message = $sanitize(message);
      service.confirm({
        title: '你确定吗？',
        message: message,
        buttons: {
          confirm: {
            label: '移除',
            className: 'btn-danger',
          },
        },
        callback: callback,
      });
    };

    service.confirmUpdate = function (message, callback) {
      message = $sanitize(message);
      service.confirm({
        title: '你确定吗？',
        message: message,
        buttons: {
          confirm: {
            label: '更新',
            className: 'btn-warning',
          },
        },
        callback: callback,
      });
    };

    service.confirmRedeploy = function (message, callback) {
      message = $sanitize(message);
      service.confirm({
        title: '',
        message: message,
        buttons: {
          confirm: {
            label: '重新部署应用程序',
            className: 'btn-primary',
          },
          cancel: {
            label: "稍后执行",
          },
        },
        callback: callback,
      });
    };

    service.confirmDeletionAsync = function confirmDeletionAsync(message) {
      return new Promise((resolve) => {
        service.confirmDeletion(message, (confirmed) => resolve(confirmed));
      });
    };

    service.confirmContainerDeletion = function (title, callback) {
      title = $sanitize(title);
      prompt({
        title: title,
        inputType: 'checkbox',
        inputOptions: [
          {
            text: '自动删除非持久存储卷<i></i>',
            value: '1',
          },
        ],
        buttons: {
          confirm: {
            label: '移除',
            className: 'btn-danger',
          },
        },
        callback: callback,
      });
    };

    service.confirmContainerRecreation = function (callback) {
      customPrompt(
        {
          title: '你确定吗?',
          message:
            "你要重新创建这个容器，任何不存在的数据都会丢失。这个容器将被删除，并创建另一个使用相同的配置容器。",
          inputType: 'checkbox',
          inputOptions: [
            {
              text: '拉取最新镜像<i></i>',
              value: '1',
            },
          ],
          buttons: {
            confirm: {
              label: '重新创建',
              className: 'btn-danger',
            },
          },
          callback: callback,
        },
        false
      );
    };

    service.confirmEndpointSnapshot = function (callback) {
      service.confirm({
        title: '你确定吗?',
        message: '触发手动刷新将轮询每个主机以检索其信息，这可能需要几秒钟。',
        buttons: {
          confirm: {
            label: '继续',
            className: 'btn-primary',
          },
        },
        callback: callback,
      });
    };

    service.confirmImageExport = function (callback) {
      service.confirm({
        title: '注意事项',
        message: '导出可能需要数分钟，在导出过程中不要离开当前页面',
        buttons: {
          confirm: {
            label: '继续',
            className: 'btn-primary',
          },
        },
        callback: callback,
      });
    };

    service.confirmServiceForceUpdate = function (message, callback) {
      message = $sanitize(message);
      customPrompt(
        {
          title: '你确定吗?',
          message: message,
          inputType: 'checkbox',
          inputOptions: [
            {
              text: '拉取镜像的最新版本<i></i>',
              value: '1',
            },
          ],
          buttons: {
            confirm: {
              label: '更新',
              className: 'btn-primary',
            },
          },
          callback: callback,
        },
        false
      );
    };

    return service;
  },
]);
